package com.csun.cmny;

import cn.jpush.api.JPushClient;
import com.csun.cmny.config.properties.CmnyProperties;
import com.csun.cmny.provider.netty.test.TcpServer;
import com.csun.cmny.provider.resolver.CmnyLocaleResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.task.TaskExecutor;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.LocaleResolver;
import tk.mybatis.spring.annotation.MapperScan;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.Locale;

@Slf4j
@EnableHystrix
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
@EnableTransactionManagement
@MapperScan("com.csun.cmny.provider.dao")
public class CmnyProviderDmcApplication implements CommandLineRunner {

	@Resource
	private CmnyProperties cmnyProperties;
	@Autowired
	private TcpServer tcpServer;
	@Resource
	private TaskExecutor taskExecutor;

	public static void main(String[] args) {
		SpringApplication.run(CmnyProviderDmcApplication.class, args);
		log.info("cmny provider dmc application start...");
	}

	@Bean
	public LocaleResolver localeResolver() {
		return new CmnyLocaleResolver();
	}

//	@Bean
//	public SleepSubClient sleepSubClient() {
//		return new SleepSubClient();
//	}

//    @Bean
//    public GatewaySubClient gatewaySubClient() {
//        return new GatewaySubClient();
//    }

	@Bean
	public ReloadableResourceBundleMessageSource messageSource() {
		Locale.setDefault(Locale.CHINA);
		ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
		messageSource.setDefaultEncoding("UTF-8");
		messageSource.setBasenames("i18n/errors/ErrorMessages", "i18n/prompt/PromptMessages",
				"i18n/validation/ValidationMessages", "classpath:ValidationMessages",
				"classpath:ErrorCodeMessages");
		return messageSource;
	}

	@Bean
	public Validator getValidator() throws Exception {
		LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
		validator.setValidationMessageSource(messageSource());
		return validator;
	}

	@Bean
	public CmnyLocaleResolver cmnyLocaleResolver() {
		return new CmnyLocaleResolver();
	}

	/**
	 * 守护佳社区版-中控台
	 */
	@Bean(name = "nursingCmnyTablet")
	public JPushClient getNursingCmnyTablet() {
		JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getNursingCmnyTablet().getMasterSecret(),
				cmnyProperties.getJpush().getNursingCmnyTablet().getAppKey());
		return jPushClient;
	}

	/**
	 * 守护佳-app
	 * @return
	 */
	@Bean(name = "nursingApp")
	public JPushClient getNursingApp() {
		JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getNursingApp().getMasterSecret(),
				cmnyProperties.getJpush().getNursingApp().getAppKey());
		return jPushClient;
	}

	/**
	 * 守护佳-中控台
	 */
	@Bean(name = "nursingTablet")
	public JPushClient getNursingTablet() {
		JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getNursingTablet().getMasterSecret(),
				cmnyProperties.getJpush().getNursingTablet().getAppKey());
		return jPushClient;
	}

	/**
	 * 社区版-中控台
	 */
	@Bean(name = "cmnyTablet")
	public JPushClient getCmnyTablet() {
		JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyTablet().getMasterSecret(),
				cmnyProperties.getJpush().getCmnyTablet().getAppKey());
		return jPushClient;
	}

	/**
	 * 社区版-中控台
	 */
	@Bean(name = "cmnyFamily")
	public JPushClient getCmnyFamily() {
		JPushClient jPushClient = new JPushClient(cmnyProperties.getJpush().getCmnyFamily().getMasterSecret(),
				cmnyProperties.getJpush().getCmnyFamily().getAppKey());
		return jPushClient;
	}

	/**
	 * life-ios平板
	 */
	@Bean(name = "cmnyLifeTablet")
	public JPushClient getCmnyLifeTablet() {
		JPushClient jPushClient = new JPushClient("a6c9be24eaf47a394e1da9e1", "2759078be3d2efce031a8968");
		return jPushClient;
	}

	@Override
	public void run(String... args) throws Exception {

//		taskExecutor.execute(() -> {
//			try {
//				Thread.sleep(5000);
//				tcpServer.run(8234);
//				log.info("tcp netty server start OK.");
//			} catch (Exception e) {
//				log.error("tcp netty server start, 出现异常={}", e.getMessage(), e);
//			}
//		});

	}
}
